package com.atguigu.webadmin.service.impl;

import com.atguigu.common.exception.SelfBusException;
import com.atguigu.common.result.ResultCodeEnum;
import com.atguigu.model.enums.ItemType;
import com.atguigu.model.pojo.*;
import com.atguigu.webadmin.mapper.*;
import com.atguigu.webadmin.service.*;
import com.atguigu.webadmin.vo.apartment.ApartmentDetailVo;
import com.atguigu.webadmin.vo.apartment.ApartmentItemVo;
import com.atguigu.webadmin.vo.apartment.ApartmentQueryVo;
import com.atguigu.webadmin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.webadmin.vo.fee.FeeValueVo;
import com.atguigu.webadmin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author 梅超凡
 * @Date 2024/11/12~20:24
 * @Description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
*/
@Service
@Slf4j
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private FeeValueMapper feeValueMapper;

    /**
     * @Author 梅超凡
     * @Date 2024/11/15~14:23
     * @Description 根据提交的的公寓vo对象进行保存或更新操作，其中配套、图片、标签、杂费等数据会单独处理
     *              如果是更新操作，则先删除原有数据，再进行保存
     *              如果是新增操作，则直接保存
    */
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        // 通过id是否为空判断是否为更新
        boolean isUpdate = apartmentSubmitVo.getId()!=null;
        Long apartmentId = apartmentSubmitVo.getId();
        // 保存或更新
        super.saveOrUpdate(apartmentSubmitVo);
        //如果是更新，则删除原有图片
        if (isUpdate){
            //1.删除图片列表
            LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
            graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphQueryWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            boolean b = graphInfoService.remove(graphQueryWrapper);
            log.info("图片列表删除完成：{}",b);

            //2.删除配套列表
            LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
            facilityQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            boolean b1 = apartmentFacilityService.remove(facilityQueryWrapper);
            log.info("配套列表删除完成：{}",b1);

            //3.删除标签列表
            LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
            labelQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            boolean b2 = apartmentLabelService.remove(labelQueryWrapper);
            log.info("标签列表删除完成：{}",b2);

            //4.删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
            feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            boolean b3 = apartmentFeeValueService.remove(feeQueryWrapper);
            log.info("杂费列表删除完成：{}",b3);
        }

        //1.插入图片列表
        // 获取图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        //如果图片列表不为空，则插入图片列表
        if (!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            //遍历图片列表
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                //设置图片所属对象类型
                graphInfo.setItemType(ItemType.APARTMENT);
                //设置图片所属对象id
                graphInfo.setItemId(apartmentSubmitVo.getId());
                //设置图片名称和地址
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                //添加到图片列表
                graphInfoList.add(graphInfo);
            }
            //批量插入图片列表
            boolean b = graphInfoService.saveBatch(graphInfoList);
            log.info("批量插入图片列表完成：{}",b);
        }

        //2.插入配套列表
        // 获取配套列表
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
        //如果配套列表不为空，则插入配套列表
        if (!CollectionUtils.isEmpty(facilityInfoIdList)){
            //创建配套列表
            ArrayList<ApartmentFacility> facilityList = new ArrayList<>();
            //遍历配套列表
            for (Long facilityId : facilityInfoIdList) {
                //创建配套对象并设置属性
                ApartmentFacility apartmentFacility = ApartmentFacility
                        //使用builder模式创建
                        .builder()
                        //设置公寓id
                        .apartmentId(apartmentId)
                        //设置配套id
                        .facilityId(facilityId)
                        //builder模式结束
                        .build();
                facilityList.add(apartmentFacility);
            }
            boolean b = apartmentFacilityService.saveBatch(facilityList);
            log.info("批量插入配套列表完成：{}",b);
        }


        //3.插入标签列表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel
                        .builder()
                        .apartmentId(apartmentId)
                        .labelId(labelId)
                        .build();
                apartmentLabelList.add(apartmentLabel);
            }
            boolean b = apartmentLabelService.saveBatch(apartmentLabelList);
            log.info("批量插入标签列表完成：{}",b);
        }


        //4.插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue
                        .builder()
                        .apartmentId(apartmentId)
                        .feeValueId(feeValueId)
                        .build();
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            boolean b = apartmentFeeValueService.saveBatch(apartmentFeeValueList);
            log.info("批量插入杂费列表完成：{}",b);
        }
    }

    /**
     * @Author 梅超凡
     * @Date 2024/11/15~11:52
     * @Description 根据page和查询条件分页查询公寓信息列表
    */
    @Override
    public IPage<ApartmentItemVo> pageItemApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        IPage<ApartmentItemVo> apartmentItemVoIPage = apartmentInfoMapper.pageItemApartmentItemByQuery(page, queryVo);
        log.info("分页查询完成：{}",apartmentItemVoIPage);
        return apartmentItemVoIPage;
    }


    /**
     * @Author 梅超凡
     * @Date 2024/11/15~14:22
     * @Description 根据id获取公寓详情
     *              包括图标、标签、配套、费用等
    */
    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        //1.查询ApartmentInfo
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo == null) {
            return null;
        }

        //2.查询GraphInfo
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);

        //3.查询LabelInfo
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);

        //4.查询FacilityInfo
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);

        //5.查询FeeValue
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);

        //创建公寓详情vo对象
        ApartmentDetailVo adminApartmentDetailVo = new ApartmentDetailVo();

        //属性对拷
        BeanUtils.copyProperties(apartmentInfo, adminApartmentDetailVo);
        //传入图片vo列表
        adminApartmentDetailVo.setGraphVoList(graphVoList);
        //传入标签vo列表
        adminApartmentDetailVo.setLabelInfoList(labelInfoList);
        //传入配套信息vo列表
        adminApartmentDetailVo.setFacilityInfoList(facilityInfoList);
        //传入杂费vo列表
        adminApartmentDetailVo.setFeeValueVoList(feeValueVoList);

        //返回公寓详细信息vo对象
        return adminApartmentDetailVo;
    }

    /**
     * @Author 梅超凡 
     * @Date 2024/11/15~14:28
     * @Description 删除公寓信息需要逐一删除图片、标签、配套、费用等
    */
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void removeApartmentById(Long id) {

        //逻辑删除公寓信息
        apartmentInfoMapper.deleteById(id);
        //指定查询条件
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(id != null,RoomInfo::getApartmentId, id);
        //查询房间
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count > 0) {
            throw new SelfBusException(ResultCodeEnum.DELETE_ERROR);
        }
        //删除图片
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        boolean b = graphInfoService.remove(graphQueryWrapper);
        log.info("删除图片信息完成：{}",b);

        //删除标签
        LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        boolean b1 = apartmentLabelService.remove(labelQueryWrapper);
        log.info("删除标签信息完成：{}",b1);

        //删除配套
        LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        boolean b2 = apartmentFacilityService.remove(facilityQueryWrapper);
        log.info("删除配套信息完成：{}",b2);

        //删除费用
        LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
        feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        boolean b3 = apartmentFeeValueService.remove(feeQueryWrapper);
        log.info("删除费用信息完成：{}",b3);

    }
}




